package com.mt.lulucai.util;

import android.util.Log;

import com.log.KLog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 有关日期工具类(extends TimeUtil)
 * <p>
 * TimeUtil主要功能有：
 * 1.各种日期类型（字符，util.Date，sql.Date，Calendar等）转换
 * 2.获取指定日期的年份，月份，日份，小时，分，秒，毫秒
 * 3.获取当前/系统日期(指定日期格式)
 * 4.获取字符日期一个月的天数
 * 5.获取指定月份的第一天,最后一天
 * <p>
 * DateUtil主要功能有：
 * 1.日期比较
 * 2.获取2个字符日期的天数差，周数差，月数差，年数差
 * 3.日期添加
 * 4.判断给定日期是不是润年
 */

public abstract class TimeUtil {

    /**
     * int类型转换为String类型
     *
     * @param time
     * @return
     */
    public static String formatTime(long time) {
        if (time != 0) {
            StringBuilder sb = new StringBuilder();
            long hour = time / 3600;
            sb.append("0" + hour).append(":");
            long minute = time % 3600 / 60;

            if (minute < 10) {
                sb.append("0" + minute);
            } else {
                sb.append(minute);
            }

//            if (minute < 10) {
//                sb.append("0" + minute).append(":");
//            } else {
//                sb.append(minute).append(":");
//            }
//            long second = time % 60;
//            if (second < 10) {
//                sb.append("0" + second);
//            } else {
//                sb.append(second);
//            }

            return sb.toString();
        } else {
            // 未知
            return "0:00";
        }

    }

    /**
     * int类型转换为String类型
     *
     * @param time
     * @return
     */
    public static String formatMTime(long time) {
        if (time != 0) {
            StringBuilder sb = new StringBuilder();

            long minute = time / 60 ;
            if (minute < 10) {
                sb.append("0" + minute);
            } else {
                sb.append(minute);
            }
            sb.append(":");
            long second = time % 60 ;
            if (second < 10) {
                sb.append("0" + second);
            } else {
                sb.append(second);
            }
            return sb.toString();
        } else {
            // 未知
            return "00:00";
        }

    }

    /**
     * int类型转换为String类型
     *
     * @param time
     * @return
     */
    public static String formatDateDes(int time) {
        Date date = new Date();
        date.setTime(time * 1000L);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * int类型转换为String类型
     *
     * @param time
     * @return
     */
    public static String formatDateDes(long time) {
        Date date = new Date();
        date.setTime(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }


    public static int getLNextongFromyyyyMMdd(String date) {
        SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        Date parse = null;
        try {
            parse = mFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (parse != null) {
            long times = parse.getTime();
            int time = (int) (times / 1000L);
//            int time = (int) (times / 1000L);
            return time;
        } else {
            return -1;
        }
    }
    public static int getLNextongFromyyyyMMdd(String date,SimpleDateFormat format) {
        Date parse = null;
        try {
            parse = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (parse != null) {
            long times = parse.getTime();
            int time = (int) (times / 1000L);
            return time;
        } else {
            return -1;
        }
    }
    public static int getCurUnixTime() {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String hehe = dateFormat.format(now);
        System.out.println(hehe);
        int time = getLNextongFromyyyyMMdd(hehe,dateFormat);
        return time;
    }

    public static String getCurDate() {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return  dateFormat.format(now);
    }

    public static String getCuTime() {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        return  dateFormat.format(now);
    }

    public static int getMinute(long time) {
        Date date = new Date(time);
        int minute = date.getMinutes();
        KLog.e("getMinute", "DATE = " + date.toString());
        return minute;
    }

    public static String getCurMonth() {
        String strFirstDay = "";
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM");

        Calendar calendar = Calendar.getInstance();

        strFirstDay = sDateFormat.format(calendar.getTime());
        return strFirstDay;
    }

    //获得本月第一天0点时间
    public static int getTimesMonthmorning() {
        Calendar cal = Calendar.getInstance();
        Log.e("time","cal.get(Calendar.MONTH) = "+cal.get(Calendar.MONTH)+"cal.get(Calendar.YEAR) = "+cal.get(Calendar.YEAR)+"cal.get(Calendar.DAY_OF_MONTH) = "+cal.get(Calendar.DAY_OF_MONTH));
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        Log.e("time","Calendar.DAY_OF_MONTH = "+Calendar.DAY_OF_MONTH+"cal.getActualMinimum(Calendar.DAY_OF_MONTH) = "+cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return (int) (cal.getTimeInMillis() / 1000);
    }

    //获得本月第一天0点时间
    public static int getTimesMonthmorning(int year,int month) {
        Calendar cal = Calendar.getInstance();
        Log.e("time","cal.get(Calendar.MONTH) = "+cal.get(Calendar.MONTH)+"cal.get(Calendar.YEAR) = "+cal.get(Calendar.YEAR)+"cal.get(Calendar.DAY_OF_MONTH) = "+cal.get(Calendar.DAY_OF_MONTH));
        cal.set(year, month-1, cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        Log.e("time","Calendar.DAY_OF_MONTH = "+Calendar.DAY_OF_MONTH+"cal.getActualMinimum(Calendar.DAY_OF_MONTH) = "+cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return (int) (cal.getTimeInMillis() / 1000);
    }

    //获得月第一天0点时间
    public static int getTimesMonthmorningByPos(int pos) {
        Calendar cal = Calendar.getInstance();
        Log.e("time","cal.get(Calendar.MONTH) = "+cal.get(Calendar.MONTH)+"cal.get(Calendar.YEAR) = "+cal.get(Calendar.YEAR)+"cal.get(Calendar.DAY_OF_MONTH) = "+cal.get(Calendar.DAY_OF_MONTH));
        cal.add(Calendar.MONTH, pos);
        Log.e("time","cal.get(Calendar.MONTH) = "+cal.get(Calendar.MONTH)+"cal.get(Calendar.YEAR) = "+cal.get(Calendar.YEAR)+"cal.get(Calendar.DAY_OF_MONTH) = "+cal.get(Calendar.DAY_OF_MONTH));
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        Log.e("time","Calendar.DAY_OF_MONTH = "+Calendar.DAY_OF_MONTH+"cal.getActualMinimum(Calendar.DAY_OF_MONTH) = "+cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return (int) (cal.getTimeInMillis() / 1000);
    }

    //获得本月最后一天24点时间
    public static int getTimesMonthnightByPos(int pos) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, pos);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        return (int) (cal.getTimeInMillis() / 1000);
    }

    //获得本月最后一天24点时间
    public static int getTimesMonthnight() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        return (int) (cal.getTimeInMillis() / 1000);
    }

    public static String getCurMonth(int pos) {
        String strFirstDay = "";
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM");

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, pos);
        strFirstDay = sDateFormat.format(calendar.getTime());
        return strFirstDay;
    }

    public static String getCurrentTimePeriod() {
        String tCurrentTimePeriod = "";
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        if (hour >= 0 && hour < 6) {
            tCurrentTimePeriod="凌晨";
        }else if (hour >= 6 && hour < 8) {
            tCurrentTimePeriod="早上";
        } else if (hour >= 8 && hour < 11) {
            tCurrentTimePeriod="上午";
        } else if (hour >= 11 && hour < 13) {
            tCurrentTimePeriod="中午";
        } else if (hour >= 13 && hour < 18) {
            tCurrentTimePeriod="下午";
        } else if (hour >= 18 && hour < 24) {
            tCurrentTimePeriod="晚上";
        }
        return tCurrentTimePeriod;
    }

    /**
     * 获取日期加天数查的日期
     * @param date
     * @param day
     * @return
     */
    public static String getDateByDay(String date,int day) {
        Date nowDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            nowDate = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar tCalendar = Calendar.getInstance();
        tCalendar.setTime(nowDate);
        tCalendar.add(Calendar.DAY_OF_MONTH, day);// 今天+1天
        return sdf.format(tCalendar.getTime());
    }
}
